using System;

namespace ntools.NetduinoGo.PlusModule.NetduinoGo
{
    public class Link
    {
        private byte[] _myUUID = new byte[] { 51, 110, 49, 191, 93, 161, 15, 70, 150, 200, 98, 43, 56, 61, 114, 13 };
        private GoBus.GoBusSerialTransport transport;

        internal Link(GoBus.GoSocket socket)
        {
            transport = new GoBus.GoBusSerialTransport("COM1", _myUUID);
            transport.FunctionCallReceived += new GoBus.GoBusSerialTransport.FunctionCallEventHandler(FunctionCall);
            transport.ResetRequestReceived += new GoBus.GoBusSerialTransport.EventHandler(ResetRequest);
        }

        public byte[] Call(byte instance, byte function, bool wait, params string[] parameters)
        {
            string param = "null";

            if (parameters.Length > 0)
            {
                param = parameters[0];

                for (int i = 1; i < parameters.Length; i++)
                {
                    param += "|" + parameters[i];
                }
            }

            return transport.CallFunction(0x14, instance, function, System.Text.Encoding.UTF8.GetBytes(param), wait);
        }

        public byte[] Call(byte instance, byte function, byte[] parameters)
        {
            return this.transport.CallFunction(0x14, instance, function, parameters, true);
        }

        private void ResetRequest(object sender, GoBus.GoBusSerialTransport.EventArgs e)
        {
            throw new NotImplementedException();
        }

        private byte[] FunctionCall(object sender, GoBus.GoBusSerialTransport.GoBusFunctionEventArgs e)
        {
            throw new NotImplementedException();
        }

        public const byte INSTANCE_MAIN = 0x00;
        public const byte COMMAND_MAIN_CREATESOCKET = 0x01;
        public const byte COMMAND_MAIN_PING = 0x02;
        public const byte COMMAND_MAIN_CREATENETWORKSTREAM = 0x03;
        public const byte COMMAND_MAIN_CREATEFILESTREAM = 0x04;

        public const byte INSTANCE_SOCKET = 0x02;
        public const byte COMMAND_SOCKET_SETSOCKETOPTION = 0x01;
        public const byte COMMAND_SOCKET_SENDTIMEOUT = 0x02;
        public const byte COMMAND_SOCKET_SEND = 0x03;
        public const byte COMMAND_SOCKET_DISPOSE = 0x04;
        public const byte COMMAND_SOCKET_CONNECT = 0x05;
        public const byte COMMAND_SOCKET_BIND = 0x06;
        public const byte COMMAND_SOCKET_LISTEN = 0x07;
        public const byte COMMAND_SOCKET_POLL = 0x08;
        public const byte COMMAND_SOCKET_ACCEPT = 0x09;
        public const byte COMMAND_SOCKET_AVAILABLE = 0x10;
        public const byte COMMAND_SOCKET_RECEIVE = 0x11;
        public const byte COMMAND_SOCKET_REMOTEENDPOINT = 0x12;

        public const byte INSTANCE_NETWORKSTREAM = 0x03;
        public const byte COMMAND_NETWORKSTREAM_READBYTE = 0x01;
        public const byte COMMAND_NETWORKSTREAM_READ = 0x02;
        public const byte COMMAND_NETWORKSTREAM_DISPOSE = 0x03;
        public const byte COMMAND_NETWORKSTREAM_CANREAD = 0x04;

        public const byte INSTANCE_DNS = 0x04;
        public const byte COMMAND_DNS_GETHOSTENTRY = 0x01;

        public const byte INSTANCE_FILESTREAM = 0x05;
        public const byte COMMAND_FILESTREAM_READ = 0x01;
        public const byte COMMAND_FILESTREAM_WRITE = 0x02;
        public const byte COMMAND_FILESTREAM_POSITION = 0x03;
        public const byte COMMAND_FILESTREAM_LENGTH = 0x04;
        public const byte COMMAND_FILESTREAM_SEEK = 0x05;
        public const byte COMMAND_FILESTREAM_SETLENGTH = 0x06;
        public const byte COMMAND_FILESTREAM_CANSEEK = 0x07;
        public const byte COMMAND_FILESTREAM_CANREAD = 0x08;
        public const byte COMMAND_FILESTREAM_CANWRITE = 0x09;
        public const byte COMMAND_FILESTREAM_DISPOSE = 0x10;
        public const byte COMMAND_FILESTREAM_FLUSH = 0x11;

        public const byte INSTANCE_FILE = 0x06;
        public const byte COMMAND_FILE_DELETE = 0x01;
        public const byte COMMAND_FILE_EXISTS = 0x02;

        public const byte INSTANCE_DIRECTORY = 0x07;
        public const byte COMMAND_DIRECTORY_CREATE = 0x01;
        public const byte COMMAND_DIRECTORY_GETENUMERATOR = 0x02;

        public const byte INSTANCE_ENUMERATOR = 0x08;
        public const byte COMMAND_ENUMERATOR_MOVENEXT = 0x01;
        public const byte COMMAND_ENUMERATOR_CURRENT = 0x02;
        public const byte COMMAND_ENUMERATOR_RESET = 0x03;
        public const byte COMMAND_ENUMERATOR_DISPOSE = 0x04;
    }
}
